#include <iostream>
#include <cstdio>
#include <csetjmp>

using namespace std;
jmp_buf g_env;


class FileError {
public:
	FileError (const string& file, int line)
		:m_file (file), m_line (line) {
			cout << "出错的位置" << m_file << ',' << m_line << endl;
		}

private:
	string m_file;
	int m_line;
};


class A{
public:
	A (void) {cout << "A::A()"<< endl;}
	~A (void) {cout << "A::~A()"<< endl;}
};

int func3 (void) {
	A a;
	FILE *fp = fopen ("xx.txt", "r");
	if (NULL == fp) {
		throw FileError(__FILE__, __LINE__); 
		//注意不会抛出两次异常, 因为第一个异常抛出之后, 后面的代码不会再执行!
		cout << "test" << endl;
		throw -1;
	}
	fclose (fp);
	return 0;
}

int func2 (void) {
	A a;
	func3();
	return 0;
}

int func1 (void) {
	A a;
	func2();
	return 0;
}

int main (void)
{
	//try/catch捕获异常
	try {
		func1();
	}
	catch (int ex) {
		if (ex == -1) {
			cout << "file open failed!" << endl;
		}
	}
	catch (FileError& ex) {
		cout << "FileError" << endl;
	}
	return 0;
}
